/* 
 * -----------------------------------------------------------------------
 * This is File I/O class for android that implements the FileIO interface.
 * 2011, by Wu Mu Xian
 * ----------------------------------------------------------------------- 
*/

package mygame.myandroidgameframework;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import android.content.res.AssetManager;
import android.os.Environment;

import mygame.basicgameframework.FileIO;

public class MyAndroidFileIO implements FileIO {
	
	AssetManager assets; // the asset manager of android
	String externalStoragePath; // store the path of the external resource
	
	// the constructor
	public MyAndroidFileIO(AssetManager assets) {
		this.assets = assets;
		this.externalStoragePath = Environment.getExternalStorageDirectory()
		       .getAbsolutePath()  + File.separator;
	}

	// read asset from APK
	public InputStream readAsset(String fileName) throws IOException {
		return assets.open(fileName);
	}

	// read file from storage
	public InputStream readFile(String fileName) throws IOException {
		return new FileInputStream(externalStoragePath + fileName);
	}

	// write file to storage
	public OutputStream writeFile(String fileName) throws IOException {
		return new FileOutputStream(externalStoragePath + fileName);
	}
}
